/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jaotwinker;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;


/**
 *
 * @author Grayson
 */
public class XyphosParser extends DefaultHandler {
    public List items;
    public List list;
    // whats the new XML? Probably won't deal with that for awhile 
    private Item tempItem;
    private Skill tempSkill;
    private String tempVal;
    private boolean functionActive=false;
    private boolean eventActive=false;
    private boolean actionActive=false;
    ExcludedItems ei;
    
    public void XyphosParser() {}
    
    public void parse(String filename, ExcludedItems ei) {
        items = new ArrayList();
        list = new ArrayList();
        this.ei=ei;
        
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            SAXParser sp = spf.newSAXParser();
            sp.parse(filename, this);
        }catch(SAXException se) {
                se.printStackTrace();
        }catch(ParserConfigurationException pce) {
                pce.printStackTrace();
        }catch (IOException ie) {
                ie.printStackTrace();
        }
    }
    
    public void characters(char[] ch, int start, int length) throws SAXException {
        tempVal = new String(ch,start,length);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        if(qName.equalsIgnoreCase("Item")) {
                //add it to the list if it isn't null and passes the test
                if(tempItem!=null) {
                    if(tempItem.testItem(ei)) items.add(tempItem);
                    tempItem=null;
                }
        }else if (qName.equalsIgnoreCase("Function")) {
            if(tempSkill!=null && tempItem!=null) {
                tempItem.Skills.add(tempSkill);
                tempSkill=null;
                functionActive=false;
            }
        }else if (qName.equalsIgnoreCase("Event")) { eventActive=false; 
        }else if (qName.equalsIgnoreCase("Action")) { actionActive=false;
        }
    }
    
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //System.out.println("Begin element:" + qName);
        if(qName.equalsIgnoreCase("Item")) { addItem(attributes); }
        else if(qName.equalsIgnoreCase("Attribute")) { addAttribute(attributes); }
        
        
        else if(qName.equalsIgnoreCase("Event")) { addEvent(attributes); }
        else if(qName.equalsIgnoreCase("Function")) { addFunction(attributes); }
        else if(qName.equalsIgnoreCase("Argument")) { 
            if(!list.contains(attributes.getValue("Value")) && functionActive) {
                list.add(attributes.getValue("Value"));
                //System.out.println("    " + attributes.getValue("Value"));
            }
            addSkill(attributes); 
        }
        
        else if(qName.equalsIgnoreCase("Action")) { addAction(attributes); }
        else if(qName.equalsIgnoreCase("Requirement")) { addRequirement(attributes); }
    }
    
    public void addItem(Attributes attributes) {
        tempItem = new Item();
        int id = Integer.parseInt(attributes.getValue("AOID"));
        if(id==274718) {
            System.out.println("Found item.");
        }
        String name = attributes.getValue("Name");
        int ql = Integer.parseInt(attributes.getValue("QL"));
        String type = attributes.getValue("ItemType");
        tempItem.set(id, name, ql, type);
    }

    public void addAttribute(Attributes attr) {      
        if(tempItem==null) {
            System.out.println("wtf: null tempitem @ addattr");
            return;
        }
        if(attr.getValue("Name").equalsIgnoreCase("Slot")) {
            tempItem.slot = attr.getValue("Value");
        }
        else if(attr.getValue("Name").equalsIgnoreCase("Icon")) {
            tempItem.icon = Integer.parseInt(attr.getValue("Value"));
        }
    }
    
    /////////////////////////
    
    public void addEvent(Attributes attr) {
        if(tempItem==null) {
            System.out.println("wtf: null tempitem @ addevent");
            return;
        }
        if(!attr.getValue("Name").equalsIgnoreCase("OnWear")) { eventActive=false; return; }
        eventActive=true;
    }
    
    public void addFunction(Attributes attr) {
        if(tempItem==null) {
            System.out.println("wtf: addFunction");
            return;
        }
        if(eventActive==false) { return; }
        if(!attr.getValue("Name").equalsIgnoreCase("Modify")) { functionActive=false; return; }
        if(!attr.getValue("Target").equalsIgnoreCase("Wearer") && !attr.getValue("Target").equalsIgnoreCase("Target")) 
        { functionActive=false; return; }
        functionActive=true;
    }
    
    public void addSkill(Attributes attr) {
        if(tempItem==null) {
            System.out.println("wtf: addskill");
            return;
        }
        if(eventActive==false || functionActive==false) { return; } 
        if(tempSkill==null) {
            tempSkill = new Skill();
            tempSkill.name = tempSkill.getName(attr.getValue("Value"));
            return;
        }
        else {
            tempSkill.value = attr.getValue("Value");
        }
        
    }
    
    /////////////////////////
    
    public void addAction(Attributes attr) {
        if(tempItem==null) {
            System.out.println("wtf: null tempitem @ action");
            return;
        }
        actionActive=true;
    }
    
    public void addRequirement(Attributes attr) {
        if(tempItem==null || attr==null) {
            System.out.println("wtf: addreq");
            return;
        }
        if(!attr.getValue("Target").equalsIgnoreCase("Self")) return;
        Requirement tempReq = new Requirement();
        tempReq.name = attr.getValue("Statistic");
        tempReq.operator = attr.getValue("Operator");
        tempReq.value = attr.getValue("Value");
        tempReq.childop = attr.getValue("ChildOp");
        tempItem.Requirements.add(tempReq);
    }
    
}


//            if(!list.contains(attributes.getValue("Statistic"))) {
//                list.add(attributes.getValue("Statistic"));
//                System.out.println("    " + attributes.getValue("Statistic"));
//            }


// if(!attr.getValue("Name").equalsIgnoreCase("Modify")) return;